<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 18/12/13
 * Time: 上午9:35
 */

namespace app\admin\controller;


use think\Controller;
use think\Db;

class Demo extends Controller
{
    // 递归方法实现无限极分类
    public function demo()
    {
        $all = Db::name('test')->select();
        $data = $this->aa($all);
        print_r($all);
        print_r($data);
        $this->assign('data',json_encode($data));
    return view();
    }


    public function aa($data,$pid =0)
    {
        $newdata =[];
        foreach ($data as $v){
            if ($v['dad'] == $pid){
                $v['nodes'] = $this->aa($data,$v['id']);
                $v['text'] = $v['name'];
                $newdata[] = $v;
            }
        }
        return $newdata;

    }

    public function add()
    {
        $request = $this->request;
        if($request->isPost()){
            $parents = $request->post('parents');
            $dad = $request->post('dad',1);
            $children = $request->post('children');
            $child = $request->post('child',2);
//            $data = [['name' => $parents,'dad' => $dad],['name' => $children,'dad' => $child]];
            $data = ['name' => $parents,'dad' => $dad];
            if (Db::name('test')->insert($data)){
                $this->success('成功',url(''));
            }else{
                $this->error('失败');
            }
        }

        if($request->isGet()){
            return view();
        }

    }



}